package org.example;

import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class CSV {
    public static String getCsvData() {
        String csv = null;
        String apiKey = "OITPBN1KWQIWAQBQ";

        String queryUrl = String.format(
                "https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_MONTHLY&symbol=BTC&market=USD&apikey=%s&datatype=csv",
                apiKey);

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(queryUrl))
                .GET()
                .build();
        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            csv = response.body();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

        return csv;
    }

    @Test
    public void 测试获取CSV数据() {
        System.out.println(getCsvData());
    }
}